#coding=utf-8
#@Time:2019\10\30 0030 15:15
#@Author:希哥
#@File:changeUserType.py

import requests
import search
import datetime

s = requests.session()
url = 'http://user-service.hupovip.cn/user-service/v1.0/member/expired'
headers = {'Content-Type':'application/json'}

'''
修改用户身份为普通用户
'''
def changeType(username):
    if username == '':
        return u'请输入微信用户名'
    else:
        user_id = search.search("SELECT id FROM hp_user.user WHERE name = '" + username + "'")
        data = ({
          "user_id": user_id,
          "operation": "et quis ullamco in",
          "source_Id": "Lorem nulla fugiat magna",
          "source_type": "d"
        })
        put = s.put(url,json=data,headers=headers)
        print put.text
        status = put.json()['msg']
        if status == 'success':
            return u'修改成功，当前用户为普通用户'
        else:
            return u'修改失败，当前用户已为普通用户'

def close(order_no):
    closeheaders = {'Authorization':'Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJvbFh2andOaVdHSVd3WmN5VHhtRmVuUFVZXzhRIiwiZXhwIjoxNTczMDI0MjA2LCJ1c2VyIjp7InVzZXJuYW1lIjoib2xYdmp3TmlXR0lXd1pjeVR4bUZlblBVWV84USIsInBhc3N3b3JkIjpudWxsLCJ1c2VyX2lkIjoiMjYzMjEyOTc3NDMyODUyMDYiLCJvcGVuX2lkIjoib3dMMzQwTjdFWlVNMVJIcDdnc2NBTTZLUGF5OCIsInVuaW9uX2lkIjoib2xYdmp3TmlXR0lXd1pjeVR4bUZlblBVWV84USIsImVuYWJsZWQiOnRydWUsImF1dGhvcml0aWVzIjpbeyJhdXRob3JpdHkiOiJVU0VSIn1dLCJhY2NvdW50X25vbl9leHBpcmVkIjp0cnVlLCJhY2NvdW50X25vbl9sb2NrZWQiOnRydWUsImNyZWRlbnRpYWxzX25vbl9leHBpcmVkIjp0cnVlfSwianRpIjoiYzRmNjU4ZjktMGJkZC00MGQzLWI1M2MtOWE1MGUxNWJhZDQ0IiwiY2xpZW50X2lkIjoiaHAtbWFsbCIsInNjb3BlIjpbImFsbCJdfQ.EUhtEF1vfA5PgENnFnXRrAWXAcCR8OVrjltrRank-x2dtR3ghaUs5i526kWa4x6_QhGTIDn4wUR5ip41boZVGzALSFmcKn7k1CEHJdKPmzoKp6099H80yVzl7bz9kIu0g52w4YKJMaQKG-UNNnX1FerzCsN22-f8vdRyjmsXj4w'}
    put = s.put('http://dev-hp-mall.hupovip.cn/v1.0/order/close/'+order_no,headers=closeheaders)
    print put.text

'''
清除用户订单和行程数据
'''
def clear(username,type):
    if username == '':
        return u'请输入微信用户名'
    else:
        user_id = str(search.search("SELECT id FROM hp_user.user WHERE name = '" + str(username) + "'"))
        if type == 'schedule':
            doClear = search.update("UPDATE hp_mall.schedule SET dt = '1' WHERE user_id = '" + user_id +"' and dt = '0'")
        if type == 'order':
            doClear = search.update("UPDATE hp_mall.orders SET dt = '1' WHERE user_id = '" + user_id + "' and dt = '0' and type = 'CERTIFICATE'")
        if type == 'all':
            doClear = search.update("UPDATE hp_mall.schedule SET dt = '1' WHERE user_id = '" + user_id + "' and dt = '0'")
            doClear = search.update("UPDATE hp_mall.orders SET dt = '1' WHERE user_id = '" + user_id + "' and dt = '0' and type = 'CERTIFICATE'")
        return u'清除成功！'

'''
修改凭证/预约/行程时间
'''
def changeTime(order_id,type):
    now = datetime.datetime.now()
    # 获取今天零点
    zeroToday = now - datetime.timedelta(hours=now.hour, minutes=now.minute, seconds=now.second,microseconds=now.microsecond)
    # 获取23:59:59
    lastToday = zeroToday + datetime.timedelta(hours=23, minutes=59, seconds=59)
    #昨日零点
    yesterday_0 = str(zeroToday - datetime.timedelta(days=1))
    #昨日23：59：59
    yesterday_23 = str(lastToday - datetime.timedelta(days=1))

    '''
        修改业务凭证时间sql
    '''
    change_biz_certificate_group = "UPDATE hp_mall.biz_certificate_group SET start_time = '" + yesterday_0 + "' , end_time = '" + yesterday_23 + "' WHERE from_id = '" + order_id + "'"

    '''
        修改中台凭证时间sql
    '''
    change_certificate_rule = "UPDATE certificate.certificate_rule SET start_time = '" + yesterday_0 + "' , end_time = '" + yesterday_23 + "' WHERE certificate_id IN (SELECT id FROM certificate.certificate WHERE from_id = '"+ order_id +"')"

    '''
        修改普通订单行程时间sql
    '''
    change_schedule = "UPDATE hp_mall.schedule SET start_time = '" + yesterday_0 + "' , end_time = '" + yesterday_23 + "' WHERE from_id = '" + order_id + "'"

    '''
        修改未预约行程时间sql
    '''
    change_schedule_no = "UPDATE hp_mall.schedule SET end_time = '" + yesterday_23 + "' WHERE from_id = '" + order_id + "'"

    '''
        修改已预约行程时间sql
    '''
    change_schedule_yes = "UPDATE hp_mall.schedule SET start_time = '" + yesterday_0 + "' , end_time = '" + yesterday_23 + "' WHERE from_id IN (SELECT reservation_record_id FROM reservation_to_order WHERE order_id = '" + order_id + "')"
    '''
        修改预约时间sql
    '''
    change_reservation_record = "UPDATE hp_mall.reservation_record SET start_time = '" + yesterday_0 + "' , end_time = '" + yesterday_23 + "' WHERE id IN (SELECT reservation_record_id FROM reservation_to_order WHERE order_id = '" + order_id + "')"

    if order_id == '':
        return u'请输入order_id!'
    #非酒店：无需预约+前置预约
    if type == 'all':
        # 修改凭证时间至昨日
        search.update(change_biz_certificate_group)

        # 修改中台凭证时间至昨日
        search.update(change_certificate_rule)

        # 修改行程时间至昨日
        search.update(change_schedule)

        return u'修改成功，凭证/行程均已修改至昨日0点-23:59:59'

    #酒店前置预约
    if type == 'before':
        # 修改预约时间至昨日
        search.update(change_reservation_record)

        # 修改已预约行程时间至昨日
        search.update(change_schedule_yes)
        return u'修改成功，行程/预约均已修改至昨日0点-23:59:59'

    # 未预约-后置预约
    if type =='after':
        # 修改凭证时间至昨日
        search.update(change_biz_certificate_group)

        # 修改中台凭证时间至昨日
        search.update(change_certificate_rule)

        # 修改行程时间至昨日
        search.update(change_schedule_no)
        return u'修改成功，凭证/行程均已修改至昨日0点-23:59:59'

    #后置预约仅预约过期，凭证未过期
    if type == 'only_after':
        # 修改预约时间至昨日
        search.update(change_reservation_record)

        # 修改中台凭证时间至昨日
        # search.update(change_certificate_rule)

        return u'修改成功，预约均已修改至昨日0点-23:59:59'

    #已预约-后置预约
    if type == 'aftered':
        #修改凭证时间至昨日
        search.update(change_biz_certificate_group)

        # 修改中台凭证时间至昨日
        search.update(change_certificate_rule)

        #修改预约时间至昨日
        search.update(change_reservation_record)

        #修改已预约行程时间至昨日
        search.update(change_schedule_yes)

        #修改未预约行程时间至昨日
        search.update(change_schedule_no)

        return u'修改成功，凭证/行程/预约均已修改至昨日0点-23:59:59'

if __name__=='__main__':
    # changeType('26321297743285206')
    # close('201909041500032822')
    # clear('Holy','all')
    changeTime('111','all')

